package FlowGraph.Transformations;

import FlowGraph.*;
import FlowGraph.Nodes.*;

//////////////////////////////////////////////////////////////////////////
//// RemoveNode
/**

        Uma transformacao para remove um no do FG. Atualiza as conexoes,
as tabelas de simbolos, e os dados do fg.

@author Ivan Jeukens
@version v 0.1 14/05/2003
*/
public class RemoveNode extends Transformation {

    public RemoveNode(FlowGraph fg) {
        super(fg);
    }

    ///////////////////////////////////////////////////////////////////
    ////                        public  methods                    ////

    public int transform(FlowGraphNode node) {

        switch(node.getType()) {

            case FlowGraphNode.BASIC_BLOCK: {
                return _removeBasicBlock((BasicBlockNode) node);
            }
            
            default: return 0;
        }
    }

    ///////////////////////////////////////////////////////////////////
    ////                        private methods                    ////

    private int _removeBasicBlock(BasicBlockNode node) {
       int oscope = node.getOpenedScopes();
       int cscope = node.getClosedScopes();

       for(int i = 0;i < node.inputSize();i++) {
            FlowGraphNode in = node.getInputNode(i);
            in.setClosedScopes(in.getClosedScopes() + cscope);

            for(int j = 0;j < node.outputSize();j++) {
                FlowGraphNode out = node.getOutputNode(j);
                _flowGraph.replaceConnection(in, node, out);
                
                if(i == 0) {
                    out.setOpenedScopes(out.getOpenedScopes() + oscope);
                }
            }
        }

        _flowGraph.removeNode(node);

        return 1;
    }

    ///////////////////////////////////////////////////////////////////
    ////                      private variables                    ////

}
